//index.js
//获取应用实例
const app = getApp()
var template = require('../../utils/tabBarTemplate/tabBarTemplate.js');


Page({
  data: {
    /*banenr */
    banner: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    windowWidth: 0,
    /*banenr end*/
    imgpath:'',
    cassification:'',//推荐分类
    prodList:'',//推荐商品
    couponList:[],//优惠券
    keyword:'',// 搜索的关键字
  },
  onLoad: function () {
   
    
    var that = this;
    //tabbar菜单
    template.tabbar("tabBar", 0, this)//0表示第一个tabbar
    var imgpath = app.globalData.imgsPath;
    that.setData({
      imgpath: imgpath
    })

    wx.getSystemInfo({
      success: function (res) {
        // console.log(res);
        that.setData({
          windowWidth: res.windowWidth
        })
      }
    })

    that.bannerList();
    that.categoriesList();
    that.commodityList();
    that.getCoupon();

   
  },
  //banner 列表
  bannerList:function(){
    var that = this;
    wx.request({
      url: app.globalData.httpsPath + '/banner',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      //请求后台数据成功  
      success: function (res) {
        // console.info(res);
        var banlist = res.data.data;
        that.setData({
          banner: banlist
        })
      }
    })
  },
  // 获取关键字
  keywordInput:function(e){
    this.setData({
      keyword: e.detail.value
    })

  },

  // 搜索商品
  srcProduct: function(){
    var that = this;
    if(!that.data.keyword) {
      wx.showToast({
        icon: "none",
        title: '请输入关键字进行查询'
      })
      return;
    }
    wx.request({
      url: app.globalData.httpsPath + '/product/search',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        keyword: that.data.keyword,
        token: wx.getStorageSync("token"),
      },
      //请求后台数据成功  
      success: function (res) {
        var prodlist = res.data.data.data;
        // console.info(res);
        that.setData({
          prodList: prodlist
        })
      }
    })
  },
  //推荐商品分类列表
  categoriesList: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpsPath + '/rec-cate',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: wx.getStorageSync("token"),
      },
      //请求后台数据成功  
      success: function (res) {
        var sortlist = res.data.data;
        // console.info(sortlist);
        that.setData({
          cassification: sortlist
        })
      }
    })
  },
  //分类点击事件
  categoriesProd: function (e) {
    var _sortid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/classification/classification?id=' + _sortid,
    })
  },
  //推荐商品列表
  commodityList: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpsPath + '/rec-product',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: wx.getStorageSync("token"),
        cate_id:''
      },
      //请求后台数据成功  
      success: function (res) {
        var prodlist = res.data.data;
        // console.info(res);
        that.setData({
          prodList: prodlist
        })
      }
    })
  },
  //单个商品点击事件
  pordInfo:function(e){
    var _prodid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/productDetails/productDetails?id=' + _prodid,
    })
  },
  //获取代金券
  getCoupon:function(){
    var that = this;
    wx.request({
      url: app.globalData.httpsPath + '/coupon',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      //请求后台数据成功  
      success: function (res) {
        var couponlist = res.data.data;
        //  console.info(res.data);
        that.setData({
          couponList: couponlist
        })
      }
    })
  },
  //加入购物车
  joinaddCart:function(e){
    var that = this;
    var _key = wx.getStorageSync("token");
    var _id = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.httpsPath + '/cart/add',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        product_id: _id,
        num: '1'
      },
      //请求后台数据成功  
      success: function (res) {
        console.info(res);
        if (res.data.status == true) {
          wx.showToast({
            title: '加入购物车成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }
      }
    })

  },
  //领取优惠券
  receiveCoupon:function(e){
    var that = this;
    var _key = wx.getStorageSync("token");
    var _couponid = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.httpsPath + '/user/coupon/add',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        coupon_id: _couponid
      },
      //请求后台数据成功  
      success: function (res) {
        console.info(res);
        if (res.data.msg !== undefined){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500,
            mask: true
          })
        }else{
          wx.showToast({
            title: '优惠券领取成功',
            icon: 'succes',
            duration: 1500,
            mask: true
          })
        }
      }
    })
  },
})
